/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhaosj
 * Date: 2010-6-3 下午01:20:29
 */

package com.array;

import java.util.Arrays;
import java.util.Comparator;

public class SortArrayByCol {
	private int ary[][];
	private MySort mySort;

	// 数据初始化
	public SortArrayByCol(int ary[][], MySort mySort) {
		this.ary = ary;
		this.mySort = mySort;
	}

	// 排序
	public void sort() {
		Arrays.sort(ary, mySort);
		printArray();
	}

	// 打印
	private void printArray() {
		System.out.println("---------Begin---------");
		for (int[] a : ary) {
			for (int i : a)
				System.out.print(i + " ");
			System.out.println();
		}
		System.out.println("---------End---------");
	}

	public static void main(String[] arg) throws Exception {
		
		int ary[][] = { {1,50}, {3,150}, {2,220}, {5,20}};
		new SortArrayByCol(ary, new MySort(0, "desc")).sort();
		new SortArrayByCol(ary, new MySort(1, "asc")).sort();

	}
}

class MySort implements Comparator<int[]> {
	
	// 想要进行排序的列数
	private int columnNumber;
	
	// 排序方式：desc or asc
	private String order;

	public MySort(int columnNumber, String order) {
		this.columnNumber = columnNumber;
		this.order = order;
	}

	public int compare(int a[], int b[]) {
		if ("desc".equals(order)) {
			return b[columnNumber] - a[columnNumber];
		} else {
			return a[columnNumber] - b[columnNumber];
		}

	}
}

